/**
 * @Description: 
 * @author 若琳
 * @date 2021/7/17
*/


const {MYSQL}  = require("../../../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");
const Pager_DB = require("../../../../../../../../ruoling-common/src/com/ruoling/common/table/Pager_DB.js");

module.exports = class MenuService{

    /**
     *  菜单列表 service
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async MenuListService(callback){
        let connect = null;
        try{
            connect = await MYSQL().connect();
            let obj = {
                sql:"select ID as id,sys_menu_name,sys_type,sys_parent_id as pid,sys_type_desc,sys_menu_type,sys_menu_icon,sys_permission_url,Create_time,sys_available  from rl_sys_menu_permisson where Is_delete = 1"
            };
            const list = await connect.connect_query_close(obj.sql);
            callback(null,list);
            return;
        }catch (e){

            if(connect != null){
                connect.close();
            }
            callback(e);
            return ;
        }
    };

    /**
     * 菜单树Service
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async MenuTreeService(callback){
        let connect = null;
        try{
            connect = await MYSQL().connect();
            let obj = {
                sql:"select ID as id ,sys_menu_name as title,sys_parent_id as parentId from rl_sys_menu_permisson where Is_delete = 1 and sys_available = 1"
            };
            const list = await connect.connect_query_close(obj.sql);
            callback(null,list);
            return;
        }catch (e){

            if(connect != null){
                connect.close();
            }
            callback(e);
            return ;
        }

    };


    /**
     * 菜单添加Service
     * @param sys_menu_name
     * @param sys_menu_type
     * @param sys_parent_id
     * @param sys_permission_url
     * @param sys_order_num
     * @param sys_menu_icon
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async MenuAddService(sys_menu_name,sys_menu_type,sys_parent_id,sys_permission_url,sys_order_num,sys_menu_icon,callback){
        let startTrans = null;

        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();
            if(sys_menu_type == "A"){
                //按钮
                let obj = {
                    sql:"insert into rl_sys_menu_permisson(sys_menu_name,sys_type,sys_parent_id,sys_menu_type,sys_permission_url,sys_order_num,sys_menu_icon,Create_time)values(?,?,?,?,?,?,?,?)",
                    param:[sys_menu_name,"permission",sys_parent_id,sys_menu_type,sys_permission_url,sys_order_num,sys_menu_icon,new Date()]
                }
                let menurow = await startTrans.query(obj.sql,obj.param);
                await startTrans.commit();
                callback(null,menurow.affectedRows);
                return ;
            }
                //按钮
                let obj = {
                    sql:"insert into rl_sys_menu_permisson(sys_menu_name,sys_type,sys_parent_id,sys_menu_type,sys_permission_url,sys_order_num,sys_menu_icon,Create_time)values(?,?,?,?,?,?,?,?)",
                    param:[sys_menu_name,"menu",sys_parent_id,sys_menu_type,sys_permission_url== null ? "#": sys_permission_url,sys_order_num,sys_menu_icon,new Date()]
                }

                let menurow = await startTrans.query(obj.sql,obj.param);
                await startTrans.commit();
                callback(null,menurow.affectedRows);
                return ;

        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    };


    /**
     * 编辑获取数据
     * @param Id
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async MenuEditDataService(Id,callback){

        let connect = null;
        try{
            connect = await MYSQL().connect();
            let obj = {
                sql:"select * from rl_sys_menu_permisson where Is_delete = 1 and sys_available = 1 and ID = "+Id+""
            };
            const data = await connect.connect_query(obj.sql);

            let obj1 = {
                sql:"select * from rl_sys_menu_permisson where Is_delete = 1 and sys_available = 1 and ID = "+data[0].sys_parent_id+""
            }

            const sys_parent_data = await connect.connect_query_close(obj1.sql);

            callback(null,{data:data[0],sys_parent_data:sys_parent_data[0]})
            return;
        }catch (e){

            if(connect != null){
                connect.close();
            }
            callback(e);
            return ;
        }
    };


    /**
     * 菜单修改Service
     * @param sys_menu_name
     * @param sys_menu_type
     * @param sys_parent_id
     * @param sys_permission_url
     * @param sys_order_num
     * @param sys_menu_icon
     * @param ID
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async MenuEditService(sys_menu_name,sys_menu_type,sys_parent_id,sys_permission_url,sys_order_num,sys_menu_icon,ID,callback){
        let startTrans = null;

        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            //按钮
            let obj = {
                sql:"update rl_sys_menu_permisson set sys_menu_name = \""+sys_menu_name+"\",sys_menu_type=\""+sys_menu_type+"\",sys_parent_id="+sys_parent_id+",sys_permission_url=\""+sys_permission_url+"\",sys_order_num="+sys_order_num+",sys_menu_icon=\""+sys_menu_icon+"\" where ID = "+ID+"",
            }

            let menurow = await startTrans.query(obj.sql);
            await startTrans.commit();

            callback(null,menurow.affectedRows);
            return ;

        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    };

    /**
     * 图标Service
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async MenuIconService(callback){
        let connect = null;
        try{
            connect = await MYSQL().connect();
            let list = await connect.connect_query_close("select * from rl_sys_dict_data where sys_dict_type = 'sys_menu_icon' ");
            callback(null,list);
            return ;
        }catch (e){
            if(connect != null){
                connect.close();
            }
            callback(e);
            return ;
        }
    }


}